package org.example.clouddemo.product.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.example.clouddemo.bean.common.entity.Product;
import org.example.clouddemo.product.service.common.param.CreateProductParam;
import org.example.clouddemo.product.service.common.param.GetDetailParam;
import org.example.clouddemo.product.service.common.param.UpdateProductParam;
import org.example.clouddemo.product.service.common.ProductService;
import org.example.common.annotation.IgnoreAuth;
import org.example.common.response.ApiResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequiredArgsConstructor
@Api(tags = "产品app接口")
@ApiSupport(order = 1)
@RestController("appProductController")
@RequestMapping("/api/app/product")
public class ProductController {


    private final ProductService productService;

    @ApiOperation(value = "产品列表", notes = "不需要token")
    @ApiOperationSupport(order = 1)
    @PostMapping("list/{page}/{pageSize}")
    @IgnoreAuth
    public ApiResult<IPage<Product>> list(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize, @RequestBody Product product) {
        Page<Product> pageInfo = new Page<>();
        pageInfo.setCurrent(page);
        pageInfo.setSize(pageSize);
        IPage<Product> result = productService.selectPageVo(pageInfo, product);
        return ApiResult.success(result);
    }

    @ApiOperation(value = "产品详情", notes = "不需要token")
    @ApiOperationSupport(order = 2)
    @PostMapping("queryOne")
    @IgnoreAuth
    public ApiResult<Product> queryOne(@Validated @RequestBody GetDetailParam param) {
        return  ApiResult.success(productService.selectOne(param.getId()));
    }

    @ApiOperation(value = "创建产品", notes = "不需要token")
    @ApiOperationSupport(order = 3)
    @PostMapping("create")
    @IgnoreAuth
    public ApiResult<Integer> create(@Validated @RequestBody CreateProductParam param) {
        Product product = BeanUtil.copyProperties(param, Product.class);
        return ApiResult.success(productService.create(product));
    }

    @ApiOperation(value = "删除产品", notes = "不需要token")
    @ApiOperationSupport(order = 3)
    @PostMapping("delete/{id}")
    @IgnoreAuth
    public ApiResult<Integer> delete(@PathVariable("id") String id) {
        return ApiResult.success(productService.delete(id));
    }

    @ApiOperation(value = "更新产品", notes = "不需要token")
    @ApiOperationSupport(order = 4)
    @PostMapping("update")
    @IgnoreAuth
    public ApiResult<Integer> update(@Validated @RequestBody UpdateProductParam param) {
        Product product = BeanUtil.copyProperties(param, Product.class);
        return ApiResult.success(productService.update(product));
    }

    @PostMapping("selectOrderByPriceDesc")
    @ApiOperation(value = "查询所有的产品并按照价格倒序")
    @ApiOperationSupport(order = 5)
    @IgnoreAuth
    public ApiResult<List<Product>> selectOrderByPriceDesc() {
        List<Product> products=productService.selectOrderByPriceDesc();
        return ApiResult.success(products);
    }
}
